package product.inurance.manage.web;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.security.pkcs11.wrapper.Constants;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@RestController
@RequestMapping("/manage")
@Slf4j
public class InuranceManageController {

    @RequestMapping("/register/instance")
    public String RegisterManage(){
        NamingService naming= null;
        try {
            //客户端简单鉴权实现或者采用阿里阿里云鉴权实现
            Properties properties = clientAuthSimple();
            //实例信息
            Instance instance = new Instance();
            //实例IP，提供给消费者进行通信的地址
            instance.setIp("127.0.0.1");
            //端口，提供给消费者访问的端口
            instance.setPort(8005);
            //权重，当前实例的权限，浮点类型（默认1.0D）
            instance.setWeight(2);
            Map<String, String> map = new HashMap<String, String>();
          /*  //netType：网络类型，这里设置的值为external（外网）
            map.put("netType", "external");
            //version Nacos版本，这里为2.0
            map.put("version", "2.0");
            instance.setMetadata(map);*/

            //关键代码 创建自己的实例
            NamingService namingService = NacosFactory.createNamingService(properties);
            namingService.registerInstance("inuraceManage.manage.test", "insurance-dev", instance);

           // naming.registerInstance("nacos_name","192.168.80.1",8080,"DEFAULT");
        } catch (NacosException e) {
           log.error(e.getMessage(),e);
        }
        return "success";
    }


    /**
     *  //客户端简单鉴权实现
     * @return
     */
    private Properties clientAuthSimple() {
        //Nacos Server连接信息
        Properties properties = new Properties();
        //Nacos服务器地址
        properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848");
        //连接Nacos服务的用户名
        properties.put(PropertyKeyConst.USERNAME, "nacos");
        //连接Nacos服务的密码
        properties.put(PropertyKeyConst.PASSWORD, "nacos");

        properties.put(PropertyKeyConst.NAMESPACE, "68e11817-64b3-441b-9d7b-8872b81cad05");
        return properties;
    }

    /**
     *  阿里云方式鉴权
     * @return
     */
    private Properties clientAuthAlibab() {
        //Nacos Server连接信息
        Properties properties = new Properties();
        //Nacos服务器地址
        properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848");
        //连接Nacos服务的用户名
        properties.put(PropertyKeyConst.ACCESS_KEY, "nacos");
        //连接Nacos服务的密码
        properties.put(PropertyKeyConst.SECRET_KEY, "nacos");
        properties.put(PropertyKeyConst.NAMESPACE, "68e11817-64b3-441b-9d7b-8872b81cad05");
        return properties;
    }
}
